package com.wao.cms.service;

import com.wao.cms.domain.Header;
import com.wao.cms.repository.HeaderRepository;
import com.wao.cms.service.dto.HeaderDTO;
import com.wao.cms.service.mapper.HeaderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * Service Implementation for managing Header.
 */
@Service
@Transactional
public class HeaderService {

    private final Logger log = LoggerFactory.getLogger(HeaderService.class);

    private final HeaderRepository headerRepository;

    private final HeaderMapper headerMapper;

    public HeaderService(HeaderRepository headerRepository, HeaderMapper headerMapper) {
        this.headerRepository = headerRepository;
        this.headerMapper = headerMapper;
    }

    /**
     * Save a header.
     *
     * @param headerDTO the entity to save
     * @return the persisted entity
     */
    public HeaderDTO save(HeaderDTO headerDTO) {
        log.debug("Request to save Header : {}", headerDTO);
        Header header = headerMapper.toEntity(headerDTO);
        header = headerRepository.save(header);
        return headerMapper.toDto(header);
    }

    /**
     * Get all the headers.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<HeaderDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Headers");
        return headerRepository.findAll(pageable)
            .map(headerMapper::toDto);
    }

    /**
     * Get one header by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public HeaderDTO findOne(Long id) {
        log.debug("Request to get Header : {}", id);
        Header header = headerRepository.findOne(id);
        return headerMapper.toDto(header);
    }

    /**
     * Delete the  header by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Header : {}", id);
        headerRepository.delete(id);
    }

    /**
     * Get headers by Website.
     *
     * @param pageable  the pagination information
     * @param websiteId the id of the Website
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<HeaderDTO> findByWebsiteId(Long websiteId, Pageable pageable) {
        log.debug("Request to get Headers by Website: {}", websiteId);
        return headerRepository.findByWebsiteId(websiteId, pageable)
            .map(headerMapper::toDto);
    }
}
